package com.handydata.antifraud.rule.funcs;

import com.handydata.antifraud.to.RiskInputTO;
import com.handydata.antifraud.to.RiskMiddleTO;
import com.handydata.antifraud.to.RiskOutputTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Optional;

/**
 * 计算反欺诈风险结果info4
 */
@Slf4j
@Component
public class CalcRiskOutputInfo4 implements Calculate {
    private static final String[] uindustries  = new String[]{
            "银行", "证券", "保险", "信托", "基金", "政府", "厅", "局", "服务中心",
            "小学", "高级中学", "高中", "初中", "初级中学", "大学", "学院", "学校"
    };

    @Override
    public void doCalc(RiskInputTO riskInputTO, RiskMiddleTO riskMiddleTO, RiskOutputTO riskOutputTO) {
        double quotient = 0;

        log.info("start calc CalcRiskOutputInfo4 uindustries{} getUnitPersonCount{}",uindustries,riskInputTO.getUnitPersonCount());
        long uIndustryCount = count(uindustries, riskInputTO.getLastUnitName());
        int unitPersonCount=riskInputTO.getUnitPersonCount()==null?0:riskInputTO.getUnitPersonCount();

        if(uIndustryCount > 0) {
            quotient = 2.5;
        } else if((unitPersonCount >= 20 && unitPersonCount <= 80)
                    || count(new String[]{}, riskInputTO.getLastUnitName()) > 0) {
            quotient = 1.7;
        } else {
            quotient = 2;
        }

        riskOutputTO.setInfo4(String.valueOf(quotient));
    }

    private long count(String[] array, String name) {
        return Arrays.asList(array).stream().filter(ind -> Optional.ofNullable(name).orElse("").contains(ind)).count();
    }

}
